package com.sff.site.node {
	import com.sff.core.sff_internal;
	import com.sff.site.abstract.AbstractNode;
	import com.sff.site.core.INode;
	import com.sff.site.core.IPath;
	import com.sff.site.events.NodeEvent;
	import com.sff.site.tree.TreeNode;
	
	import flash.net.URLVariables;	
	
	use namespace sff_internal;

	/**
	 * dispatched when the acti of the node
	 * 
	 * @see NodeEvent#CHANGE
	 */[Event( name = "_nodeChange", type = "com.sff.site.events.NodeEvent" )]
	
	/**
	 * A node is an image of a treenode object provided to a module to acces and modify site tree state
	 * 
	 */
	public class Node extends AbstractNode {
		
		public override function isActive() :Boolean {
			return _referer.isActive();	
		}
		
		/**
		 * go to the page defined by this Node
		 */
		public override function activate( params : URLVariables = null ) : void {
			_referer.activate( params );	
		}
		
		override public function getDefaultId() : String {
			return _referer.getDefaultId();
		}

		override public function getParams() : URLVariables {
			return _referer.getParams( );
		}

		override public function path( getDefaults : Boolean = false ) : IPath {
			return _referer.path( getDefaults );
		}

		public function Node( parent : Node, referer : TreeNode ) {
			super( parent );
			_referer = referer;
			_id = _referer.getId( );
			register();
		}
		
		override public function getCurrentChild() : INode {
			for each (var n : INode in _childs) {
				if( n.isActive() ) return n;
			}
//			if (_default )
//				return getChild( _default );
			return null;
		}

		public override function describe(descriptor : NodeDescriptor) : void {
//			_id = descriptor.id;
		}
		


		private function register() : void {
			_referer.addEventListener( NodeEvent.PARAM_CHANGE, _refBEvent );
			_referer.addEventListener( NodeEvent.CHANGE , _refBEvent );
			_referer.addEventListener( NodeEvent.CHANGE , _refCEvent, true );
			_referer.addEventListener( NodeEvent.CHILD_CHANGE , _refBccEvent );
			_referer.addEventListener( NodeEvent.CHILD_CHANGE , _refCccEvent, true );
		}
		
		private function _refBEvent(event : NodeEvent) : void {
			dispatchEvent( event );
		}

		private function _refCEvent(event : NodeEvent) : void {
//			if( _childs.length > 0 )
				dispatchEvent( event );
		}
		
		private function _refBccEvent(event : NodeEvent) : void {
			if( _childs && _childs.length > 0 )
				dispatchEvent( event );
		}

		private function _refCccEvent(event : NodeEvent) : void {
			if( _childs && _childs.length > 0 )
				dispatchEvent( event );
		}

		/**
		 * internal use only
		 * called by the Tree when this node become unactive and should be eligible for GC, as well as the module connect to him.
		 * @see Tree.dispose() 
		 */
		sff_internal function dispose() : void {
			_dispose();
			_referer.removeEventListener( NodeEvent.PARAM_CHANGE, _refBEvent );
			_referer.removeEventListener( NodeEvent.CHANGE , _refBEvent );
			_referer.removeEventListener( NodeEvent.CHANGE , _refCEvent, true );
			_referer.removeEventListener( NodeEvent.CHILD_CHANGE , _refBccEvent );
			_referer.removeEventListener( NodeEvent.CHILD_CHANGE , _refCccEvent, true );
			_referer = null;
		}

		sff_internal function getReferer() : TreeNode {
			return _referer;
		}

		private var _referer : TreeNode;


	}
}
